<?php

namespace App\Http\Controllers\H5\Shop;

use App\Http\Controllers\Controller;
use App\Service\ShopService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GoodsController extends Controller
{
    /**
     * 积分商城商品列表
     * @param Request $request
     * @param ShopService $shopService
     * @return array
     */
    public function index(Request $request, ShopService $shopService)
    {
        $scores = $request->scores ?? 'asc';
        $level = $request->level ?? 0;
        $uid = $request->uid;
        $start = $request->start ?? 0;
        $limit = $request->limit ?? 10;
        $list = $shopService->getGoodsList($scores, $level, $start, $limit);
        $num = $shopService->getGoodsNum($level);
        $scores = $shopService->getScores($uid);
        return ApiResponse(['list' => $list, 'num' => $num, 'user_scores' => $scores]);

    }

    /**
     * 商品详情页
     * @param Request $request
     * @param ShopService $shopService
     * @return array
     */
    public function show(Request $request, ShopService $shopService)
    {
        $goodsId = $request->goods_id;
        $uid = $request->uid;
        $goodsInfo = $shopService->getGoodsInfo($goodsId);
        $userScores = $shopService->getScores($uid);
        $addressNum = DB::table('address')->where('uid', $uid)->count();
        $hasAddress = $addressNum ? 1 : 0;
        return ApiResponse(['goods_info' => $goodsInfo, 'user_scores' => $userScores, 'has_address' => $hasAddress]);
    }

}
